package com.exam.online.system.service.impl;

import com.exam.online.system.constant.StatusCode;
import com.exam.online.system.dao.CourseDAO;
import com.exam.online.system.dao.QuestionDAO;
import com.exam.online.system.entity.Course;
import com.exam.online.system.entity.Question;
import com.exam.online.system.entity.Teacher;
import com.exam.online.system.model.IdAndNameModel;
import com.exam.online.system.model.QuestionTypeModel;
import com.exam.online.system.service.QuestionService;
import com.exam.online.system.util.PageModel;
import com.exam.online.system.util.ResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: zp
 * @date: 2019-05-06 15:22
 */
@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionDAO questionDAO;
    @Autowired
    private CourseDAO courseDAO;

    @Override
    public Integer addQuestion(Integer type, String title, String optiona, String optionb, String optionc, String optiond, String answer, Integer score, Integer grade, Integer courseId) {

        Question question = new Question();
        question.setType(type);
        question.setTitle(title);

        if (type == 1) {
            //选择题
            question.setOptiona(optiona);
            question.setOptionb(optionb);
            question.setOptionc(optionc);
            question.setOptiond(optiond);

        }
        question.setAnswer(answer);
        question.setScore(score);
        question.setGrade(grade);
        question.setCourseId(courseId);
        question.setState(1);
        question.setCreate(new Date());
        return questionDAO.insert(question);
    }

    /**
     * 通过课程，年级，题目类型查找题目，用于教师组装试卷
     *
     * @param courseName
     * @param gradeId
     * @return
     */
    @Override
    public ResponseModel findQuestionByCondition(String courseName, Integer gradeId) {
        Course course = courseDAO.selectByCourseNameAndGradeId(courseName, gradeId);
        List<Question> a = questionDAO.selectByCourseIdAndType(course.getId(), 1);
        List<Question> b = questionDAO.selectByCourseIdAndType(course.getId(), 2);
        List<Question> c = questionDAO.selectByCourseIdAndType(course.getId(), 3);
        QuestionTypeModel questionTypeModel = new QuestionTypeModel();
        List<IdAndNameModel> choiceList = new ArrayList<>();
        List<IdAndNameModel> judgeList = new ArrayList<>();
        List<IdAndNameModel> shortAnswerList = new ArrayList<>();
        a.forEach(e -> {
            IdAndNameModel idAndNameModel = new IdAndNameModel();
            idAndNameModel.setId(e.getId());
            idAndNameModel.setName(e.getTitle());
            choiceList.add(idAndNameModel);
        });
        b.forEach(e -> {
            IdAndNameModel idAndNameModel = new IdAndNameModel();
            idAndNameModel.setId(e.getId());
            idAndNameModel.setName(e.getTitle());
            judgeList.add(idAndNameModel);
        });
        c.forEach(e -> {
            IdAndNameModel idAndNameModel = new IdAndNameModel();
            idAndNameModel.setId(e.getId());
            idAndNameModel.setName(e.getTitle());
            shortAnswerList.add(idAndNameModel);
        });
        questionTypeModel.setChoiceList(choiceList);
        questionTypeModel.setJudgeList(judgeList);
        questionTypeModel.setShortAnswerList(shortAnswerList);
        return ResponseModel.buildFromStateCode(StatusCode.SUCCESS, questionTypeModel);
    }

    @Override
    public PageModel getAllQuestion(Integer pageSize, Integer pageNo) {
        PageModel<Question> pageModel = new PageModel<>();
        pageModel.setPageNo(pageNo);
        pageModel.setPageSize(pageSize);
        List<Question> paperList = questionDAO.getAllQuestion((pageNo - 1) * pageSize, pageSize);
        Integer total = questionDAO.getAllQuestionCount();
        pageModel.setDatas(paperList);
        pageModel.setTotalPages(total / pageSize + 1);
        pageModel.setTotalRecords(total);
        return pageModel;


    }


}
